from pymysql import connect
from utils.constants import *

conn = connect(
    host=HOST, user=USERNAME, password=PASSWORD, db=DATABASE_NAME, charset=CHARSET
)
cursor = conn.cursor()


def query(sql, params=None, many=False):
    """
    构建数据库执行语句（用于查询，大量数据更新、保存）
    :param sql:
    :param params:
    :param many: 是否使用批量执行
    :return:
    """
    if many:
        cursor.executemany(sql, params)
    else:
        cursor.execute(sql, params)
    conn.commit()
    res = cursor.fetchall()
    return res
